package jp.shuitic.hatebwebhook.twitter.handler;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * はてなブックマークWEB Hookを受け取るサーブレット
 * @author SHUICHI
 *
 */
@SuppressWarnings("serial")
public class HatebHookServlet extends HttpServlet {
	
	private HatebHook hatebHook = null;
	static private Logger logger = Logger.getLogger("jp.shuitic");
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		HatebHookChecker checker = new HatebHookCheckerImpl();
		HatebHookExecute execute = new HatebHookExecuteImpl();
		hatebHook = new HatebHook(checker, execute);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String username = req.getParameter("username");
			String title = req.getParameter("title");
			String url = req.getParameter("url");
			String status = req.getParameter("status");
			String comment = req.getParameter("comment");
			String timestamp = req.getParameter("timestamp");
			String is_private = req.getParameter("is_private");
			String key = req.getParameter("key");
			boolean result = hatebHook.hook(username, key, url, title, status, timestamp, comment, is_private);
			if (username != null) {
				logger.info("username:"+username);
			} else {
				logger.info("username:null");
			}
			logger.info("result:" + Boolean.toString(result));
		} catch (ServletException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}
